from django.core.management.base import BaseCommand
from apps.menu.models import SysMenu

class Command(BaseCommand):
    help = 'Import default menus into the SysMenu table'

    def handle(self, *args, **options):
        # 菜单数据
        menus_data = [
            {'name': '首页', 'icon': 'home', 'parent_id': None, 'order_num': 1, 'path': '/home', 'component': 'HomePage', 'menu_type': 'M', 'perms': 'home.view', 'remark': '首页菜单'},
            {'name': '用户管理', 'icon': 'user', 'parent_id': None, 'order_num': 2, 'path': '/user', 'component': 'UserManagement', 'menu_type': 'M', 'perms': 'user.manage', 'remark': '用户管理菜单'},
            {'name': '角色管理', 'icon': 'role', 'parent_id': 2, 'order_num': 1, 'path': '/user/roles', 'component': 'RoleManagement', 'menu_type': 'C', 'perms': 'role.manage', 'remark': '角色管理菜单'},
            {'name': '权限管理', 'icon': 'permissions', 'parent_id': 2, 'order_num': 2, 'path': '/user/permissions', 'component': 'PermissionManagement', 'menu_type': 'C', 'perms': 'permission.manage', 'remark': '权限管理菜单'},
            {'name': '设置', 'icon': 'settings', 'parent_id': None, 'order_num': 3, 'path': '/settings', 'component': 'SettingsPage', 'menu_type': 'M', 'perms': 'settings.manage', 'remark': '系统设置菜单'},
            {'name': '日志管理', 'icon': 'log', 'parent_id': 5, 'order_num': 1, 'path': '/settings/logs', 'component': 'LogManagement', 'menu_type': 'C', 'perms': 'log.manage', 'remark': '日志管理菜单'},
            {'name': '按钮管理', 'icon': 'button', 'parent_id': 2, 'order_num': 3, 'path': '/user/buttons', 'component': 'ButtonManagement', 'menu_type': 'F', 'perms': 'button.manage', 'remark': '按钮管理菜单'},
        ]

        # 插入数据
        menu_objects = []
        for menu_data in menus_data:
            menu = SysMenu(
                name=menu_data['name'],
                icon=menu_data['icon'],
                parent_id=menu_data['parent_id'],
                order_num=menu_data['order_num'],
                path=menu_data['path'],
                component=menu_data['component'],
                menu_type=menu_data['menu_type'],
                perms=menu_data['perms'],
                remark=menu_data['remark']
            )
            menu_objects.append(menu)

        # 批量插入
        SysMenu.objects.bulk_create(menu_objects)

        # 输出结果
        self.stdout.write(self.style.SUCCESS(f'Successfully imported {len(menu_objects)} menus'))
